using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Collections;
using System.IO;

namespace GotLyrics
{
    public static class Common
    {
        public static string GetAssemblyFileVersion(Assembly asm)
        {
            if (asm == null)
                throw (new ArgumentNullException("asm"));

            AssemblyFileVersionAttribute versionAttribute = (AssemblyFileVersionAttribute)(asm.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false)[0]);

            return versionAttribute.Version;
        }

        public static ILyricGrabber[] GetLyricGrabbers()
        {
            ArrayList tmpArray = new ArrayList();

            Assembly asmExec = Assembly.GetExecutingAssembly();
            string[] files = Directory.GetFiles(Path.GetDirectoryName(asmExec.Location), "*.dll");

            foreach (string file in files)
            {
                Assembly asm = Assembly.LoadFile(file);

                Type[] types = asm.GetTypes();

                foreach (Type t in types)
                {
                    if (t.IsAbstract)
                        continue;

                    if (typeof(ILyricGrabber).IsAssignableFrom(t))
                    {
                        tmpArray.Add(Activator.CreateInstance(t));
                    }
                }
            }

            if (tmpArray.Count > 0)
            {
                ILyricGrabber[] ret = new ILyricGrabber[tmpArray.Count];
                tmpArray.CopyTo(ret);
                return ret;
            }

            return null;
        }
    }
}
